package com.lz.football.module.shop
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ShopEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.type.GoodsType;
	
	import common.BaseView.BaseView;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;

	/**
	 *商城购买界面 
	 * @author Administrator
	 * 
	 */	
	public class ShopBuyView extends BaseView
	{
		/**
		 *消耗 
		 */
		private var cost:TextField;
		/**
		 *购买数量  
		 */
		private var num:TextField;
		/**
		 *左 
		 */
		private var leftBtn:SimpleButton;
		/**
		 *右 
		 */
		private var rightBtn:SimpleButton;
		/**
		 *确定按钮 
		 */
		private var sureBtn:SimpleButton;
		/**
		 *取消 
		 */
		private var cancelBtn:SimpleButton;
		/**
		 *球员背景 
		 */
		private var playerBg:MovieClip;
		/**
		 * 
		 */
		private var _bitmap:Bitmap;
		/**
		 *物品名称  
		 */
		private var goodsName:TextField;
		/**
		 *购买数量 
		 */
		private var buyNum:int = 1;
		/**
		 * 
		 */
		private var _goodsVo:GoodsVo;
		public function ShopBuyView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.shop.buyView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.buyBg;
			isDrag = true;
			isCloseBtn = true;
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			cost = symbolSkin.cost;
			num = symbolSkin.num;
			leftBtn = symbolSkin.leftBtn;
			rightBtn = symbolSkin.rightBtn;
			sureBtn = symbolSkin.sureBtn;
			cancelBtn = symbolSkin.cancelBtn;
			goodsName = symbolSkin.goodsName;
			goodsName.mouseEnabled = false;
			num.text = "" + buyNum;
			playerBg =  Reflection.createInstance("com.lz.res.PlayerColorBg");
			playerBg.x = 12;
			playerBg.y = 46;
			_bitmap = new Bitmap();
			_bitmap.x = 13;
			_bitmap.y = 47;
			moveClose(_viewWidth - 30,2);
//			this.addChild(_bitmap);
			onResizeHandler();
			addEvtListener();
			
		}
		/**
		 * 设置数据 
		 * @param $vo
		 * 
		 */
		public function setData($vo:GoodsVo):void
		{
			_goodsVo = $vo;
			calculateCost();
			goodsName.text = $vo.name + "";
			_bitmap.bitmapData = $vo.bitmapData;
			if($vo.type == GoodsType.PLAYERCARD)
			{
				this.addChild(playerBg);
				playerBg.gotoAndStop($vo.color);
			}
			this.addChild(_bitmap);
			
			
		}
		override public function addEvtListener():void
		{
			leftBtn.addEventListener(MouseEvent.CLICK,onLeftHandler);
			rightBtn.addEventListener(MouseEvent.CLICK,onRightHandler);
			cancelBtn.addEventListener(MouseEvent.CLICK,onCloseHandler);
			sureBtn.addEventListener(MouseEvent.CLICK,onsureHandler);
		}
		
		protected function onsureHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_goodsVo.cashpoint * buyNum > AppData.getInstance().clubDatamanager.clubInfo.cashpoint)
			{
//				Alert.showErrorTip("点券不足");
				Alert.noCashAlert("现有点券不足"+_goodsVo.cashpoint * buyNum+ "点，是否前往充值？");
				return;
			}
			AppEventDispatcher.getInstance().dispatchEvent(new ShopEvent(ShopEvent.BUY_GOODS,{id:_goodsVo.gid,type:_goodsVo.type,num:buyNum}));
		}
		
		protected function onRightHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(buyNum >= 10)return;
			buyNum++;
			calculateCost();
		}
		
		protected function onLeftHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(buyNum<=1)return;
			buyNum--;
			calculateCost();
		}
		private function calculateCost():void
		{
			num.text = "" + buyNum;
			cost.text = "" + _goodsVo.cashpoint * buyNum;
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();	
			leftBtn.removeEventListener(MouseEvent.CLICK,onLeftHandler);
			rightBtn.removeEventListener(MouseEvent.CLICK,onRightHandler);
			cancelBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			sureBtn.removeEventListener(MouseEvent.CLICK,onsureHandler);
		
		}
		override public function destroyView():void
		{
			removeEvtListener();
			playerBg = null;
			super.destroyView();
		}
	}
}